package com.niezhipeng.topics;

import com.niezhipeng.utils.ConnectFactoryUtils;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.nio.charset.StandardCharsets;

public class EmitLogTopic {

    private static final String EXCHANGE_NAME = "topic_logs";

    public static void main(String[] argv) throws Exception {
        ConnectionFactory factory = new ConnectionFactory();
        ConnectFactoryUtils.setFactoryConfig(factory);
        try (Connection connection = factory.newConnection();
             Channel channel = connection.createChannel()) {
            // 声明一个主题模式交换机
            channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.TOPIC);

            String routingKeyA = "machineA.info";
            String messageA = "Hello World!";
            channel.basicPublish(EXCHANGE_NAME, routingKeyA, null, messageA.getBytes(StandardCharsets.UTF_8));
            System.out.println(" [x] Sent '" + routingKeyA + "':'" + messageA + "'");

            String routingKeyB = "machineB.info";
            String messageB = "Hello World!";
            channel.basicPublish(EXCHANGE_NAME, routingKeyB, null, messageB.getBytes(StandardCharsets.UTF_8));
            System.out.println(" [x] Sent '" + routingKeyB + "':'" + messageB + "'");
        }
    }
}

